package org.west.sky.frame.activiti.study.chapter17;

import org.activiti.engine.repository.ProcessDefinition;
import org.activiti.engine.runtime.ProcessInstance;
import org.activiti.engine.task.Task;
import org.west.sky.frame.activiti.study.util.ActivitiEngineUtil;

import java.util.HashMap;
import java.util.Map;
import java.util.Random;

/**
 * author: chz
 * date: 2025/6/6
 * description: 事件子流程
 */
public class RunDemo17_1_2 extends ActivitiEngineUtil {

    public static void main(String[] args) {
        RunDemo17_1_2 runDemo16_1_2 = new RunDemo17_1_2();
        runDemo16_1_2.run();
    }

    public void run() {
        loadActivitiConfigAndInitEngine("activiti.job.xml");

        ProcessDefinition processDefinition = deployByClassPathResource("processes/chapter17/Chapter17_1_2.bpmn20.xml");

        ProcessInstance processInstance = runtimeService.startProcessInstanceById(processDefinition.getId());

        Task task1 = taskService.createTaskQuery().processInstanceId(processInstance.getId()).singleResult();
        System.out.println("====当前流程：" + processInstance.getId() + "进行到：" + task1.getName() + "：任务节点");
        taskService.complete(task1.getId());

        Task task2 = taskService.createTaskQuery().processInstanceId(processInstance.getId()).singleResult();
        System.out.println("====当前流程：" + processInstance.getId() + "进行到：" + task2.getName() + "：任务节点");
        Map<String, Object> variables = new HashMap<>();
        variables.put("krState", true);
        taskService.complete(task2.getId(), variables);

        while (true) {
            Task task3 = taskService.createTaskQuery().processInstanceId(processInstance.getId()).singleResult();
            if (null == task3) {
                break;
            }
            System.out.println("====当前流程：" + processInstance.getId() + "进行到：" + task3.getName() + "：任务节点");
            Task task = taskService.createTaskQuery().taskId(task3.getId()).singleResult();
            if (null != task) {
                taskService.complete(task.getId());
            }
        }


        closeEngine();
    }
}
